package com.mlx.permission.util;

import java.util.Collection;
import java.util.Map;

/**
 * @description:
 * @DateTime: 2021-11-03 14:41
 * @author: mlx
 * @version: 1.0
 **/
public class PermissionUtil {

    private static final String foreignKeysRegex = "[a-zA-z_][a-zA-z0-9_]{0,}:[a-zA-z_][a-zA-z0-9_]{0,}:[a-zA-z_][a-zA-z0-9_]{0,}";

    public static boolean isEmpty(Object obj){
        if (obj == null){
            return true;
        }
        if (obj instanceof String){
            return "".equals(obj);
        }
        if (obj instanceof Object[]){
            return ((Object[]) obj).length == 0;
        }
        if (obj instanceof Collection){
            return ((Collection<?>) obj).isEmpty();
        }
        if (obj instanceof Map){
            return ((Map<?, ?>) obj).isEmpty();
        }
        return false;
    }

    public static boolean isNotEmpty(Object obj){
        return !(isEmpty(obj));
    }

    public static boolean isForeignKeysFormat(String str){
        return str.matches(foreignKeysRegex);
    }

    public static void throwException(String format, Object... objects){
        throw new RuntimeException(String.format(format, objects));
    }
}
